using MediatR;
using Microsoft.EntityFrameworkCore;
using WeatherHistory.Data.Repositories.Interfaces;

namespace WeatherHistory.Api.CommandsQueries.Queries.RegionTypes.Search;

public class SearchRegionTypesQueryHandler : IRequestHandler<SearchRegionTypesQueryRequest, SearchRegionTypesQueryResponse>
{
    private readonly IRegionTypeRepository _regionTypeRepository;

    public SearchRegionTypesQueryHandler(IRegionTypeRepository regionTypeRepository)
    {
        _regionTypeRepository = regionTypeRepository;
    }

    public async Task<SearchRegionTypesQueryResponse> Handle(SearchRegionTypesQueryRequest request, CancellationToken cancellationToken)
    {
        var response = new SearchRegionTypesQueryResponse();
        
        response.Items = await _regionTypeRepository.AsQueryable()
            .Skip(request.From)
            .Take(request.Size)
            .Select(x => new SearchRegionTypesQueryDto
            {
                Id = x.Id,
                Name = x.Name
            })
            .ToListAsync(cancellationToken);
        
        return response;
    }
}